home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / tools.lha / tools / error.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  1KB  |  68 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Error function. fmt is a printf-style format string.
  6. / The formatted message is written to the error
  7. / stream and then the function calls exit(1).
  8. include <stream.h>
  9. include <stdarg.h>
  10.  
  11. xtern void exit(int);
  12.  
  13. oid error(const char *fmt ...)
  14.  
  15.    va_list ap;
  16.    va_start(ap, fmt);
  17.    char ch;
  18.  
  19.    // loop across format string
  20.    while (ch = *fmt++)
  21. // output normal chars
  22. if (ch != '%')
  23.     cerr.put(ch);
  24.  
  25. else
  26.     // found % sequence
  27.     switch (ch = *fmt++)
  28.     {
  29.     // %% becomes a single %
  30.     case '%':
  31.         cerr.put('%');
  32.         break;
  33.  
  34.     // output string
  35.     case 's':
  36.         {
  37.         char *s = va_arg(ap, char*);
  38.         cerr << s;
  39.         }
  40.         break;
  41.  
  42.     // output decimal integer
  43.     case 'd':
  44.         {
  45.         int s = va_arg(ap, int);
  46.         cerr << s;
  47.         }
  48.         break;
  49.  
  50.     // output character
  51.     case 'c':
  52.         {
  53.         int s = va_arg(ap, int);
  54.         cerr.put(s);
  55.         }
  56.         break;
  57.  
  58.     default:
  59.         cerr << "\nunknown % sequence: %" <<
  60.         chr(ch) << "\n";
  61.         break;
  62.     }
  63.  
  64.    // all done
  65.    va_end(ap);
  66.    exit(1);
  67.  
  68.